package com.hemaapp.yjnh.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaUtil;
import com.hemaapp.yjnh.BaseApplication;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.activity.ShowLargeImageActivity;
import com.hemaapp.yjnh.bean.Blog;
import com.hemaapp.yjnh.bean.Img;
import com.hemaapp.yjnh.bean.ImgItemsBean;
import com.hemaapp.yjnh.bean.User;
import com.hemaapp.yjnh.listener.OnLaokeClickListener;
import com.hemaapp.yjnh.utils.ImageUtils;
import com.hemaapp.yjnh.view.ImageGridView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

public class LaokeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private ArrayList<Blog> datas;
    private Context mContext;
    private OnLaokeClickListener clickListener;
    private String typename = "0";//type=0,1,2,3,4,5,6对应全部,求助,农技,养生,补贴,其他,易货

    public LaokeAdapter(Context context, ArrayList<Blog> datas) {
        this.mContext = context;
        this.datas = datas;
    }

    public void setClickListener(OnLaokeClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public void setTypename(String typename) {
        this.typename = typename;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == -1) {
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.listitem_empty_base, parent, false);
            return new EmptyViewHolder(itemView);
        } else {
            View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_laoke, parent, false);
            return new ViewHolder(itemView);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (datas.size() == 0) {
            if (holder instanceof EmptyViewHolder)
                ((EmptyViewHolder) holder).tvEmtpy.setText("暂无内容");
        } else {
            if (holder instanceof ViewHolder) {
                ((ViewHolder) holder).setData(datas.get(position));
            }
        }
    }

    @Override
    public int getItemCount() {
        return datas.size() == 0 ? 1 : datas.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (datas.size() == 0)
            return -1;
        else
            return 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        @Bind(R.id.iv_avatar)
        ImageView ivAvatar;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.tv_date)
        TextView tvDate;
        @Bind(R.id.tv_del)
        TextView tvDel;
        @Bind(R.id.iv_zhiding)
        ImageView ivZhiding;
        @Bind(R.id.tv_title)
        TextView tvTitle;
        @Bind(R.id.tv_content)
        TextView tvContent;
        @Bind(R.id.image_grid)
        ImageGridView imageGrid;
        @Bind(R.id.tv_zan)
        TextView tvZan;
        @Bind(R.id.tv_comment)
        TextView tvComment;

        Blog blog;
        AddImageAdapter imageAdapter;
        ArrayList<ImgItemsBean> replyImgs = new ArrayList<>();

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (clickListener != null && blog != null)
                        clickListener.OnItemClickListener(blog);
                }
            });
            tvDel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (clickListener != null && blog != null)
                        clickListener.OnDeleteClickListener(blog);
                }
            });
            tvZan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (clickListener != null && blog != null)
                        clickListener.OnZanClickListener(blog);
                }
            });
            tvComment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (clickListener != null && blog != null)
                        clickListener.OnCommentClickListener(blog);
                }
            });
        }

        public void setData(Blog blog) {
            this.blog = blog;
            ImageUtils.loadAvatar(mContext, blog.getAvatar(), ivAvatar);
            //显示帖子内容，并将其中emoji表情进行转换
            HemaUtil.SetMessageTextView(mContext, tvContent, blog.getContent());
            HemaUtil.SetMessageTextView(mContext, tvTitle, blog.getName());
            tvName.setText(blog.getNickname());
            //2016.10.17 start 修改帖子时间
            tvDate.setText(HemaUtil.transTimeChat(blog.getRegdate()));
//            tvDate.setText(blog.getRegdate());
            //2016.10.17 end 修改帖子时间
            tvZan.setText(blog.getGoodcount());
            tvComment.setText(blog.getReplycount());
            if (blog.getGoodflag().equals("1")) {// 点赞标记	0无 1有
                tvZan.setCompoundDrawablesWithIntrinsicBounds(
                        mContext.getResources().getDrawable(R.drawable.thumbs_up_orange), null, null, null);
            }else {
                tvZan.setCompoundDrawablesWithIntrinsicBounds(
                        mContext.getResources().getDrawable(R.drawable.thumbs_up_grey), null, null, null);
            }
            //是否是置顶帖子
            if (typename.equals("0")) {
                if (blog.getTopkeytype().equals("1")) {
                    ivZhiding.setVisibility(View.VISIBLE);
                }else {
                    ivZhiding.setVisibility(View.INVISIBLE);
                }
            }else {
                if (blog.getTopflag().equals("1")) {
                    ivZhiding.setVisibility(View.VISIBLE);
                }else {
                    ivZhiding.setVisibility(View.INVISIBLE);
                }
            }
            //如果是自己的帖子，那么显示删除按钮
            final User user = BaseApplication.getInstance().getUser();
            if (user != null && blog.getClient_id().equals(user.getId())) {
                tvDel.setVisibility(View.VISIBLE);
            }else {
                tvDel.setVisibility(View.GONE);
            }
            //显示图片
            replyImgs.clear();
            replyImgs.addAll(blog.getImgItems());
            if (replyImgs.size() > 0) {
                imageGrid.setVisibility(View.VISIBLE);
                refreshImages();
            }else {
                imageGrid.setVisibility(View.GONE);
            }
        }
        private void refreshImages() {
            //这里必须每次都重新初始化，不然在recyclerview复用的时候，会导致图片错乱
//            if (imageAdapter == null) {
                imageAdapter = new AddImageAdapter(mContext, replyImgs);
                imageGrid.setAdapter(imageAdapter);
                imageAdapter.setShowDelete(false);
                imageAdapter.setShowAdd(false);
                imageAdapter.setImageClickListener(new AddImageAdapter.OnImageClickListener() {
                    @Override
                    public void onImageClickListener(int position) {
                        ArrayList<Img> imgs = new ArrayList<Img>();
                        for (int i = 0; i < replyImgs.size(); i++) {
                            ImgItemsBean item = replyImgs.get(i);
                            String imgurl_detail = item.getImgurlbig();
                            Img img = new Img(imgurl_detail);
                            imgs.add(img);
                        }

                        Intent intent = new Intent();
                        intent.setClass(mContext, ShowLargeImageActivity.class);
                        intent.putExtra("imgs", imgs);
                        intent.putExtra("position", position + 1);
                        mContext.startActivity(intent);
                    }

                    @Override
                    public void onDeleteClickListener(int position, ImgItemsBean imageItem) {
                    }
                });
//            } else {
//                imageAdapter.notifyDataSetChanged();
//            }
        }
    }
}
